package com.bookmanager.www.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bookmanager.www.common.Constants.PreferentialConstant;
import com.bookmanager.www.common.DTO.GiteeDTO;
import com.bookmanager.www.common.DTO.TranslateDTO;
import com.bookmanager.www.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/11/11/17:06
 * @Description:
 */
@RestController
@RequestMapping("/fl-common")
@Api(tags = "通用插件")
@Slf4j
public class CommonController {

    private static final String giteeBase = "https://gitee.com/";

    //fl1906249647/contribution_timeline?url=";
//    "fl1906249647&contribution_timeline&scope=my&day=&start_date=2&end_date=&year=&limit=100"


    public static final String base= "http://api.fanyi.baidu.com/api/trans/vip/translate?q=";


    @ApiOperation(value = "获取gitee贡献图")
    @PostMapping("/getGiteeContributionByUserName")
    Result getContributionByUserName(@RequestBody GiteeDTO giteeDTO)
    {
        String contribution = HttpUtil.get(giteeBase + giteeDTO.getUsername()
                +"/contribution_timeline?url="+giteeDTO.getUsername() + "&contribution_timeline&scope=my&day=&start_date=" +
                giteeDTO.getStartDate() + "&end_date=" + giteeDTO.getEndDate() + "&year=&limit=" + giteeDTO.getLimit());

        log.warn(contribution);

        List<Object> objects = JSONUtil.toList(contribution, Object.class);

//        JSONObject jsonObject = JSONUtil.parseObj(contribution);

        return Result.succ("获取Url成功",objects);
    }


    @ApiOperation(value = "百度翻译",notes = "")
    @PostMapping("/baiduTranslate")
    /**
     * Step1. 拼接字符串1：
     * 拼接appid=2015063000000001+q=apple+salt=1435660288+密钥=12345678得到字符串1：“2015063000000001apple143566028812345678”
     * Step2. 计算签名：（对字符串1做md5加密）
     * sign=md5(2015063000000001apple143566028812345678)，得到sign=f89f9594663708c1605f3d736d01d2d4
     */
    Result baiduTranslate(@RequestBody TranslateDTO translate)
    {
          String temp = PreferentialConstant.translateId+translate.getQuery()+"fl1906"+PreferentialConstant.translateSecret;
          String sign = SecureUtil.md5(temp);
          String url = base+translate.getQuery()
                  +"&from="+translate.getFrom()
                  +"&to="+translate.getTo()
                  +"&appid="+PreferentialConstant.translateId+"&salt=fl1906&sign="+sign;
          log.info(url);
//        String s = HttpUtil.get(url);
//        JSONObject tran = JSONUtil.parseObj(s);
//        log.info(String.valueOf(tran));
//        log.info(tran.getStr("trans_result"));
//        String dst = JSONUtil.parseObj(tran.getStr("trans_result")).getStr("dst");
//        log.info(dst);
//        String src = JSONUtil.parseObj(tran.getStr("trans_result")).getStr("src");
//        log.info(src);

        return Result.succ("获取Url成功",url);
    }
}
